$(document).ready(function () {
    $(".dp-article-box").fadeOut(0);
    $(".js-add-dp-box,.js-hf-article-pl").click(function () {
        $(".dp-article-box").not($(this).next()).slideUp('fast');
        $(this).next().slideToggle(400);
    });

    tweet_comment();
    friendlyLinkData();
    tweet_read_add();
});


/**
 * 动弹阅读数量+1
 */
function tweet_read_add() {
    var tweetId = $("#tweetId").val();
    $.ajax({
        url: '/tweet/add/read/'+tweetId,
        type: 'post',
        dataType: 'json',
        success: function (d) {

        },error:function (d) {
        }
    });
}

/**
 * 回复评论
 */
function replyTweetComment(obj,tweetId,replyUserId) {
    if(logined()){
        layui.use('layer', function () {
            var layer = layui.layer;

            var content = $(obj).parent().children('textarea').val();
            if(!content){
                layer.msg("回复内容不能为空",{icon:5});
                return;
            }
            $.ajax({
                url: '/u/add/comment/'+tweetId,
                data:{"content":content,"replyUserId":replyUserId},
                type: 'post',
                dataType: 'json',
                success: function (d) {
                    if (d.code==0) {
                        layer.msg("回复成功~",{icon:6});
                        tweet_comment();
                    }else{
                        layer.msg(d.msg?d.msg:"回复失败",{icon:5});
                    }
                },error:function (d) {
                    layer.msg(d.msg?d.msg:"回复失败",{icon:5});
                }
            });
        });
    }else{
        showLogin();
        return;
    }
}


/**
 * 发表评论
 */
function addTweetComment(obj,tweetId) {
    if(logined()){
        layui.use('layer', function () {
            var layer = layui.layer;
            var content = $(obj).parent().children('textarea').val();
            if (!content) {
                layer.msg("评论内容不能为空",{icon:5});
                return;
            }
            $.ajax({
                url: '/u/add/comment/' + tweetId,
                data: {"content": content},
                type: 'post',
                dataType: 'json',
                success: function (d) {
                    if (d.code == 0) {
                        layer.msg("评论成功~",{icon:6});
                        tweet_comment();
                    }else{
                        layer.msg(d.msg?d.msg:"评论失败",{icon:5});
                    }
                },error:function (d) {
                    layer.msg(d.msg?d.msg:"评论失败",{icon:5});
                }
            });
        });
    }else{
        showLogin();
        return;
    }
}


/**
 * 该博客评论列表
 */
function tweet_comment() {
    $("#commentsListBody").html("");
    layui.use('flow', function(){

        var flow = layui.flow;
        flow.load({
            elem: '#commentsListBody'
            ,done: function(page, next){
                var $ = layui.jquery;
                var lis = [];
                $.get('/tweet/comments/'+$("#tweetId").val(),{"number":page}, function(res){
                    if(res.page){
                        layui.each(res.page.records, function(index, item){

                            var replyId = item.replyId,reply_html='',user = res.usersMap[item.userId];
                            if(replyId){
                                var reply = res.usersMap[item.replyId];
                                reply_html ='<a href="/u/'+reply.username+'" target="_blank">@'+reply.username+'</a> ';;
                            }
                            var html= '<div class="dp-list-box">'+
                                '<div class="dl-user ">'+
                                '<ul>'+
                                '<li>'+
                                '<a href="/u/'+user.username+'" target="_blank">'+
                                '<img src="'+user.headimg+'">'+
                                '</a>'+
                                '</li>'+
                                '</ul>'+
                                '<div class="one-pl-content">'+
                                '<p class="content">'+
                                '<span class="name">'+user.username+'</span>'+
                                '<a href="/u/'+user.username+'" target="_blank"></a>&nbsp;&nbsp;'+
                                '<span class="author-content">'+reply_html+item.content+'</span>'+
                                '</p>'+
                                '</div>'+
                                '</div>'+
                                '</div>'+
                                '</div>'+

                                '<div class="pl-box-btm">'+
                                '<div class="article-type pull-right">'+
                                '<div class="icon-like-prompt">'+
                                '<i class="icon icon-like active"></i><span class="c1">+1</span>'+
                                '</div>'+
                                '<div class="icon-no-like-prompt">'+
                                '<i class="icon icon-no-like active"></i><span class="c1">+1</span>'+
                                '</div>'+
                                '<ul>'+
                                '<li class="js-icon-like" data-type="like">' +
                                '<i class="icon icon-like "></i><span class="like">2</span></li>'+
                                '<li class="js-no-icon-like" data-type="no-like">' +
                                '<i class="icon icon-no-like "></i><span class="like">1</span></li>'+
                                '</ul>'+
                                '</div>'+
                                '<a onclick="showPTextArea(this)"><div class="btn-dp transition js-add-dp-box"><i class="icon icon-dp"></i>回复</div></a>'+
                                '<div class="pl-form-box dp-article-box">'+
                                '<textarea class="form-control" placeholder="请文明回复~"></textarea>'+
                                '<button class="btn btn-article js-article-dp" onclick="replyTweetComment(this,'+item.tweetId+',\''+item.userId+'\')">回复</button>'+
                                '</div><br><br>';

                            lis.push(html);
                        });
                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }
        });
    });
}

function showPTextArea(obj) {
    if($(obj).next('div').css("display")=='none'){
        $(obj).next().css({"display":"block"});

    }else{
        $(obj).next().css({"display":"none"});
    }
}

/**
 * 友情链接
 */
function friendlyLinkData() {
    $("#friendlyLinkList").html("");
    layui.use('flow', function () {

        var flow = layui.flow;
        flow.load({
            elem: '#friendlyLinkList',
            end:" ",
            done: function (page, next) {
                var $ = layui.jquery;
                var lis = [];
                $.post('/friendly/link/list', null, function (res) {
                    if (res.page) {
                        layui.each(res.page.records, function (index, item) {
                            var desc ='';
                            if(item.desc){
                                desc = '<span class="point">&bull;</span>' +
                                    '<a href="'+item.url+'" target="_blank">'+item.desc.substr(0, 20)+'</a>' ;
                            }
                            lis.push('<li>' +
                                '<div class="mod-thumb">' +
                                '<a href="'+item.url+'" target="_blank">' +
                                '<img src="'+item.pic+'">' +
                                '</a>' +
                                '</div>' +
                                '<div class="project-content">' +
                                '<a href="'+item.url+'" class="c2" target="_blank">'+item.company+'</a>' +
                                desc +
                                '</div>' +
                                '</li>'
                            );
                        });

                        next(lis.join(''), page < res.page.pages);
                    }

                });
            }

        });
    });
}

function tweet_praise(tweetId) {
    if(!logined()){
        showLogin();
        return;
    }
    if($("#praiseCountNum").prev().hasClass("active")){
        tweet_praise_del(tweetId)
    }else{
        tweet_praise_add(tweetId);
    }
}

/**
 * 给该动弹点赞
 */
function tweet_praise_add(tweetId) {
    $.ajax({
        url: '/tweet/add/praise/'+tweetId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                var value = $("#praiseCountNum").text();
                $("#collectCount").text(Number(value)+Number(1));
                $("#praiseCountNum").text(Number(value)+Number(1));
                $("#praiseCountNum").prev().addClass("active").css({"background-color":"#FFFFFF"});
            }
        },error:function (d) {

        }
    });
}

/**
 * 取消点赞
 * @param tweetId
 */
function tweet_praise_del(tweetId) {
    $.ajax({
        url: '/tweet/cut/praise/'+tweetId,
        type: 'post',
        dataType: 'json',
        success: function (d) {
            if (d.code==0) {
                var value = $("#praiseCountNum").text();
                $("#praiseCountNum").text(Number(value)-Number(1));
                $("#collectCount").text(Number(value)+Number(1));
                $("#praiseCountNum").prev().removeClass("active");
            }
        },error:function (d) {
        }
    });
}
